package com.dormitory.dao;

import com.dormitory.Util.C3P0Util;
import com.dormitory.entity.User;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserConnectimpl implements com.dormitory.dao.UserConnect {
    @Override
    public User findByUsername(String username) {
        Connection connection = C3P0Util.getConnection();
        String sql = "select * from user where username = '"+username+"'";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery(sql);
           if (resultSet.next()){
               username =  resultSet.getString(1);
               String password =  resultSet.getString(2);
               String limit=resultSet.getString(3);
               String name=resultSet.getString(4);
               return new User(username,password,limit,name);
           }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
           C3P0Util.release(connection,statement,resultSet);
        }
        return null;
    }
}
